-- FirstRenownRender

local ClassRef = app.mvc.createRenderClass(...)

function ClassRef.create(node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewBagRenownFirstNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self:setContentSize(self._ui["Panel"]:getContentSize())

	self._ui["Panel"]:addTouchEventListener(function(sender, eventType)
		if eventType == ccui.TouchEventType.ended then
			self:setSelected( not self._isSelected )
			executeFunc(self._onClick, self)
		end
	end)

	self._ui["Panel/btn_shop"]:addClickEventListener(function()
		if self._singleId then
			if UD:isNewGotRenown( self._singleId ) then
				UD:removeFromNewGotRenown( self._singleId )
			end
			executeFunc(self.refreshRedPoint, self)
			local fames = GD:queryFameBySubKey(self._data.id)
			if fames and fames[1] then
				app:sendMsg("PrestigeShopController", "openView", { shopId = tonumber(fames[1].shopID) })
			end
		end
	end)

	self._isSelected = false -- 是否选中、默认没有

	self._image = self._ui["Panel/Image"]
	self._name = self._ui["Panel/name"]

	self:refresh()
end

function ClassRef:refresh()
	if self._data then
		local iconPath = GD:getImagePath( Res.renownBgPath, self._data.bgName )
		self._image:loadTexture( iconPath )

		local iTotalCount = GD:getFameTotalCount( self._data.id )
		local iAlreadyCount = UD:getFameCountBySubKey( self._data.id )
		local famePercent = iAlreadyCount / iTotalCount * 100
		local famePercentStr = string.format("%.2f", famePercent)
		if "0.00" == famePercentStr then
			famePercentStr = "0"
		end
        local fameLevelStr = ""
        local nameStr
        if #self._data.subDatas <= 0 then --没有二级菜单，一级菜单显示等级文本
            local fames = GD:queryFameBySubKey(self._data.id)
			if fames and fames[1] then
				self._singleId = tonumber(fames[1].ID)
                local level,exp,totalExp,levelName,fameName = GD:getFameNameLevelExp(self._singleId, true)
                if tonumber(totalExp) == 0 then
			        fameLevelStr = " (MAX)"
		        else
			        fameLevelStr = " (" .. exp .. "/" .. totalExp .. ")"
		        end
                nameStr = table.concat( {self._data.name, " ", levelName} )
            end
        else
            nameStr = table.concat( { self._data.name, " ", famePercentStr, "%" } )
        end
        self._ui["Panel/num"]:setString(fameLevelStr)
		self._name:setString( nameStr )
        --[[if #self._data.subDatas > 0 then
            self._name:setTextColor(cc.c3b(255, 225, 132))
        else
            self._name:setTextColor(display.COLOR_WHITE)
        end--]]

		self:refreshRedPoint()
        self._ui["Panel/num"]:setVisible(#self._data.subDatas <= 0)
        self._ui["Panel/img_1"]:setVisible(#self._data.subDatas > 0)
        self._ui["Panel/img_2"]:setVisible(#self._data.subDatas > 0)
        self._ui["Panel/btn_shop"]:setVisible(#self._data.subDatas <= 0)
        self._ui["Panel"]:setTouchEnabled(#self._data.subDatas > 0) --如果没有二级菜单取消一级菜单的Touch事件
	end
end

function ClassRef:refreshRedPoint()
	if self._data then
		local newFlag = false
		if self._singleId then
			if UD:isNewGotRenown( self._singleId ) then
				newFlag = true
			end
		end
		if not newFlag then
			for k,v in pairs( self._data.subDatas ) do
				if UD:isNewGotRenown( v.id ) then
					newFlag = true
					break
				end
			end
		end
		self:setNewFlagVisible( newFlag )
	end
end

-- 是否选中
function ClassRef:isSelected()
	return self._isSelected
end
-- 选中应该高亮、即setBright( true )
function ClassRef:setSelected( b )
	self._isSelected = b

    if self._data then
        if #self._data.subDatas > 0 then
	        self._ui["Panel/img_1"]:setVisible( b )
	        self._ui["Panel/img_2"]:setVisible( not b )
        end
    else
        self._ui["Panel/img_1"]:setVisible( b )
	    self._ui["Panel/img_2"]:setVisible( not b )
    end
end

-- new标记可见性
function ClassRef:setNewFlagVisible(b)
	self._ui["Panel/dot"]:setVisible( b )
end

function ClassRef:setClickHandler(func)
	self._onClick = func
end

return ClassRef
